<?php

/**
 * 计划任务晒单
 * File Name：SunsingleController.php
 * File Encoding：UTF-8
 * File New Time：2014-10-6 10:52:54
 */
class SunsingleController extends YzwController {
	private $key = 'members_sunsingle';
    public function actionIndex_bak() {
        exit;
        set_time_limit(600);
        header("Content-type: text/html; charset=utf-8");
        $data = array();
        //从redis读取50条
        for ($i = 0; $i < 50; $i++) {
            $infoList = RedisCluster::getInstance()->pop($this->key);
            if (!empty($infoList)) {
                $data[] = $infoList;
            }
        }
        print_r($data);
        //晒单
        foreach ($data as $key => $info) {
            if (empty($info)) {
                continue;
            }
            $objModel = MembersSunsingleModel::model()->findByPk($info);
            if(!$objModel || $objModel->status == 1){
            	continue;
            }
            $objSalesModel = SalesDataModel::model()->find(array('condition' => 'orders_number = :orders_number', 'params' => array('orders_number' => $objModel->ordersn)));
            //找不到此订单的销售记录
            if(!$objSalesModel){
            	$objModel->status = 2;
            	$objModel->save(false,array('status'));
            	continue;
            }
            if($objModel->post($objSalesModel, $objModel->uid)){
            	$objModel->status = 1;           	
            }else{
            	//晒单失败
            	$objModel->status = 2;
            }
            //只是计划任务里如果晒单成功或者失败则发一条系统通知
            if($objModel->status == 1){
                $message = '晒单成功';
            }else{
                $message = '晒单失败';
            }
            $params = array(
                'title' => '系统通知',
                'content' => "您好,您的订单号：{$objModel->ordersn}，{$message}",
            );
            MessageModel::systemNotice($objModel->uid, $params);
            $objModel->save(false,array('status')); 
        }
        echo "<br/>处理完毕！";
    }
    public function actionIndex() {
        set_time_limit(600);
        header("Content-type: text/html; charset=utf-8");
        $today = strtotime('today');
        //如果处理的是3天前的数据则找不到记录的话就更为失败,其它的则不处理
        //$time_before_3 = strtotime('-3 day');
        $time_before_3 = $today - 3*86400;        
        $objModels = MembersSunsingleModel::model()->findAll(array('condition' => "status = :status", 'params' => array('status' => 0)));
        foreach($objModels as $objModel){
            $objSalesModel = SalesDataModel::model()->find(array('condition' => 'orders_number = :orders_number', 'params' => array('orders_number' => $objModel->ordersn)));
            //找不到此订单的销售记录 如果是今天的则不处理，如果是昨天以前的数据则标记
            if(!$objSalesModel){
                if($objModel->add_time < $time_before_3){
                    $objModel->status = 2;
                    $objModel->save(false,array('status'));
                }else{
                    continue;
                }               
            }else{
                if($objModel->post($objSalesModel, $objModel->uid)){
                    $objModel->status = 1;              
                }else{
                    //晒单失败
                    $objModel->status = 2;
                }
                $objModel->save(false,array('status'));     
            }
            //只是计划任务里如果晒单成功或者失败则发一条系统通知
            if($objModel->status == 1){
                $message = '晒单成功';
            }else{
                $message = '晒单失败';
            }
            $params = array(
                'title' => '系统通知',
                'content' => "您好,您的订单号：{$objModel->ordersn}，{$message}",
            );
            MessageModel::model()->systemNotice($objModel->uid, $params);
            $objModel->save(false,array('status'));                     
        }
        echo "<br/>处理完毕！";
    }
}
